package com.woniu108.valueAdded.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.valueAdded.client.PersonClient;
import com.woniu108.valueAdded.model.SignIn;
import com.woniu108.valueAdded.model.UserVip;
import com.woniu108.valueAdded.service.SignInService;
import com.woniu108.valueAdded.service.UserVipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/signIn")
@Api(tags = "签到信息接口")
public class SignInController {


    @Resource
    private RedisUtil redisUtil;
    @Resource
    private SignInService signInService;
    @Resource
    private PersonClient personClient;
    @Resource
    private UserVipService userVipService;
    public String getUserId(String token){
        String refreshToken=redisUtil.getString(token);
        String userId= JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        return userId;
    }


    /**
     * 会员用户签到获取会员积分
     * @param token
     * @return newScore 签到后现有积分
     */
    @GetMapping("/userSignIn")
    @ApiOperation("会员用户签到获取会员积分,单次5分,每天限1次")
    public Result<BigDecimal> userSignIn(@RequestHeader String token){
        String userId = this.getUserId(token);
        //判断用户是否为vip用户,非vip用户不可获取会员积分
        Result<UserInfoDto> result = personClient.getByUserId(userId);
        if(result.getCode()!=200) throw new WoniuException(result.getMsg(),result.getCode());
        UserVip userVip = userVipService.getById(userId);
        if(ObjectUtil.isEmpty(userVip)||userVip.getVipEndTime().isBefore(LocalDateTime.now())){
            throw new WoniuException("非会员,无法获取会员积分",10001);
        }
        //获取上次签到时间
        QueryWrapper<SignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        //时间升序排列签到记录时间
        wrapper.orderByDesc("create_time");
        SignIn sign = signInService.getOne(wrapper);
        //若无数据,则为首次签到
        if(ObjectUtil.isEmpty(sign)) {
            SignIn signIn = new SignIn();
            signIn.setCreateTime(LocalDateTime.now());
            signIn.setSignInNum(1);
            signIn.setUserId(userId);
            signInService.save(sign);
        }else{
            //若上次签到在今日,则不允许再次签到
            if(sign.getCreateTime().toLocalDate().equals(LocalDateTime.now().toLocalDate())) throw new WoniuException("今日已签到",10001);
            //更新签到后信息
            sign.setLastSignInTime(LocalDateTime.now());
            sign.setSignInNum(sign.getSignInNum()+1);
            signInService.updateById(sign);
        }
        //会员积分增加
        BigDecimal newScore = userVip.getVipScore().add(BigDecimal.valueOf(StaticData.SIGN_IN_SCORE_PER_TIME));
        userVip.setVipScore(newScore);
        userVipService.updateById(userVip);
        return Result.getResult(BigDecimal.class).setMsg("签到成功").setData(newScore);
    }
}

